/*
 * Text.cpp
 *
 *  Created on: Jul 18, 2009
 *      Author: kurt
 */

#include "Text.h"
#include "DrawingCanvas.h"
#include <pspgu.h>
#include <pspgum.h>
#include <cstring>
#include <string>
#include <sstream>
using namespace std;
using namespace objdraw; 


Text::~Text() 
{
	
}
Text::Text()
{
	start = end = Location(0,0);
	color = Color(0,0,0);
	hidden = false;
	canvas = NULL;
	Font::initIntraFont();
	text = "";
	font = NULL;
	alternate = NULL;
}
Text::Text(const Text & r) 
{
	start = r.start;
	end = r.end;
	color = r.color;
	hidden = r.hidden;
	canvas = r.canvas;
	Font::initIntraFont();
	text = r.text;
	font = r.font;
	alternate = r.alternate;
	if(canvas != NULL)
		canvas->getCanvasContent().addDrawable(this);
}
Text::Text(bool b, float x, float y, Font * f, DrawingCanvas * canvas)
{
	start = end = Location(x,y);
	color = Color(255,255,255);
	hidden = false;
	canvas = canvas;
	Font::initIntraFont();
	text = b ? "true" : "false";
	font = f;
	if(f != NULL)
		end = Location(f->measureText(text.c_str()) + start.x, f->getFont()->size + start.y);
	alternate = NULL;
	if(canvas != NULL)
		canvas->getCanvasContent().addDrawable(this);
}
Text::Text(bool b, Location origin, Font * f, DrawingCanvas * canvas)
{
	start = end = origin;
	color = Color(255,255,255);
	hidden = false;
	canvas = canvas;
	Font::initIntraFont();
	text = b ? "true" : "false";
	font = f;
	if(f != NULL)
		end = Location(f->measureText(text.c_str()) + start.x, f->getFont()->size + start.y);
	alternate = NULL;
	if(canvas != NULL)
		canvas->getCanvasContent().addDrawable(this);
}
Text::Text(char c, float x, float y, Font * f, DrawingCanvas * canvas)
{
	start = end = Location(x,y);
	color = Color(255,255,255);
	hidden = false;
	canvas = canvas;
	text = c;
	font = f;
	if(f != NULL)
		end = Location(f->measureText(text.c_str()) + start.x, f->getFont()->size + start.y);
	alternate = NULL;
	if(canvas != NULL)
		canvas->getCanvasContent().addDrawable(this);	
}
Text::Text(char c, Location origin, Font * f, DrawingCanvas * canvas)
{
	start = end = origin;
	color = Color(255,255,255);
	hidden = false;
	canvas = canvas;
	text = c;
	font = f;
	if(f != NULL)
		end = Location(f->measureText(text.c_str()) + start.x, f->getFont()->size + start.y);
	alternate = NULL;
	if(canvas != NULL)
		canvas->getCanvasContent().addDrawable(this);
}
Text::Text(int i, float x, float y, Font * f, DrawingCanvas * canvas)
{
	start = end = Location(x,y);
	color = Color(255,255,255);
	hidden = false;
	char buff[250];
	snprintf(buff,sizeof(buff),"%i",i);
	text = buff;
	font = f;
	canvas = canvas;
	if(f != NULL)
		end = Location(f->measureText(text.c_str()) + start.x, f->getFont()->size + start.y);
	alternate = NULL;
	if(canvas != NULL)
		canvas->getCanvasContent().addDrawable(this);
}
Text::Text(int i, Location origin, Font * f, DrawingCanvas * canvas)
{
	start = end = origin;
	color = Color(255,255,255);
	hidden = false;
	char buff[250];
	snprintf(buff,sizeof(buff),"%i",i);
	text = buff;
	font = f;
	canvas = canvas;
	if(f != NULL)
		end = Location(f->measureText(text.c_str()) + start.x, f->getFont()->size + start.y);
	alternate = NULL;
	if(canvas != NULL)
		canvas->getCanvasContent().addDrawable(this);
}
Text::Text(long l, float x, float y, Font * f, DrawingCanvas * canvas)
{
	start = end = Location(x,y);
	color = Color(255,255,255);
	hidden = false;
	char buff[250];
	snprintf(buff,sizeof(buff),"%ld",l);
	text = buff;
	font = f;
	canvas = canvas;
	if(f != NULL)
		end = Location(f->measureText(text.c_str()) + start.x, f->getFont()->size + start.y);
	alternate = NULL;
	if(canvas != NULL)
		canvas->getCanvasContent().addDrawable(this);
}
Text::Text(long l, Location origin, Font * f, DrawingCanvas * canvas)
{
	start = end = origin;
	color = Color(255,255,255);
	hidden = false;
	char buff[250];
	snprintf(buff,sizeof(buff),"%ld",l);
	text = buff;
	font = f;
	canvas = canvas;
	if(f != NULL)
		end = Location(f->measureText(text.c_str()) + start.x, f->getFont()->size + start.y);
	alternate = NULL;
	if(canvas != NULL)
		canvas->getCanvasContent().addDrawable(this);
}
Text::Text(Object * obj, float x, float y, Font * f, DrawingCanvas * canvas)
{
	start = end = Location(x,y);
	color = Color(255,255,255);
	hidden = false;
	if(obj != NULL)
		text = obj->toString();
	else
		text = "NULL";
	font = f;
	canvas = canvas;
	if(f != NULL)
		end = Location(f->measureText(text.c_str()) + start.x, f->getFont()->size + start.y);
	alternate = NULL;
	if(canvas != NULL)
		canvas->getCanvasContent().addDrawable(this);
}
Text::Text(Object * obj, Location origin, Font * f, DrawingCanvas * canvas)
{
	start = end = origin;
	color = Color(255,255,255);
	hidden = false;
	if(obj != NULL)
		text = obj->toString();
	else
		text = "NULL";
	font = f;
	canvas = canvas;
	if(f != NULL)
		end = Location(f->measureText(text.c_str()) + start.x, f->getFont()->size + start.y);
	alternate = NULL;
	if(canvas != NULL)
		canvas->getCanvasContent().addDrawable(this);
}
Text::Text(string str, float x, float y, Font * f, DrawingCanvas * canvas)
{
	start = end = Location(x,y);
	color = Color(255,255,255);
	hidden = false;
	text = str;
	font = f;
	canvas = canvas;
	if(f != NULL)
		end = Location(f->measureText(text.c_str()) + start.x, f->getFont()->size + start.y);
	alternate = NULL;
	if(canvas != NULL)
		canvas->getCanvasContent().addDrawable(this);
}
Text::Text(string str, Location origin, Font * f, DrawingCanvas * canvas)
{
	start = end = origin;
	color = Color(255,255,255);
	hidden = false;
	text = str;
	font = f;
	canvas = canvas;
	if(f != NULL)
		end = Location(f->measureText(text.c_str()) + start.x, f->getFont()->size + start.y);
	alternate = NULL;
	if(canvas != NULL)
		canvas->getCanvasContent().addDrawable(this);
}
Text::Text(const char * str, float x, float y, Font * f, DrawingCanvas * canvas)
{
	start = end = Location(x,y);
	color = Color(255,255,255);
	hidden = false;
	text = str;
	font = f;
	canvas = canvas;
	if(f != NULL)
		end = Location(f->measureText(text.c_str()) + start.x, f->getFont()->size + start.y);
	alternate = NULL;
	if(canvas != NULL)
		canvas->getCanvasContent().addDrawable(this);
}
Text::Text(const char * str, Location origin, Font * f, DrawingCanvas * canvas)
{
	start = end = origin;
	color = Color(255,255,255);
	hidden = false;
	text = str;
	font = f;
	canvas = canvas;
	if(f != NULL)
		end = Location(f->measureText(text.c_str()) + start.x, f->getFont()->size + start.y);
	alternate = NULL;
	if(canvas != NULL)
		canvas->getCanvasContent().addDrawable(this);
}
Font * Text::getFont()
{
	return font;
}
string & Text::getText()
{
	return text;
}
void Text::setText(string str)
{
	text = str;
}
float Text::getWidth()
{
	if(font != NULL)
		return font->measureText(text.c_str());
	return 0;
}
void Text::render()
{
	if(hidden) return;
	if(font != NULL)
	{
		if(alternate != NULL)
			font->setAlternateFont(alternate);
		font->printf(start.x,start.y,text.c_str());
	}
}
